package com.zzx.boot.servlet.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.boot.entity.table.Post;
import com.zzx.boot.entity.table.PostClass;
import com.zzx.boot.entity.vo.ClassVo;
import com.zzx.boot.entity.vo.PostVo;
import com.zzx.boot.mapper.PostClassMapper;
import com.zzx.boot.servlet.PostClassService;
import com.zzx.boot.servlet.PostService;
import com.zzx.boot.servlet.UtilsService;
import com.zzx.boot.utils.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tan
 * @version 1.0
 * @date 2023/7/8 14:31
 */
@Service
public class PostClassServiceImpl extends ServiceImpl<PostClassMapper, PostClass> implements PostClassService{

    @Resource
    private PostService postService;
    @Resource
    private UtilsService utilsService;


    @Override
    public R getPostClass() {
        LambdaQueryWrapper<PostClass> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PostClass::getStatus,1);
        wrapper.select(PostClass::getId,PostClass::getClassname);
        List<PostClass> list = baseMapper.selectList(wrapper);
        List<ClassVo> vos = list.stream().map(item -> new ClassVo(item.getId().toString(), item.getClassname())).collect(Collectors.toList());
        return R.success(vos);
    }

    @Override
    public List<PostVo> postByPostClassPage(Long classId, Long current) {
        // 通过分类id获取所有的文章id
        Page<Post> page = new Page(current,10);

        LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Post::getPostClassId,classId);
        postService.page(page,wrapper);

        List<PostVo> collect = page.getRecords().stream().map(item -> {
            PostVo postVo = new PostVo();
            postService.getPostList(false,postVo,item);
            return postVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<PostVo> postByUserPage(Long userId, Long current) {
        // 通过分类id获取所有的文章id
        Page<Post> page = new Page(current,10);

        LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Post::getUserId,userId);
        postService.page(page,wrapper);

        List<PostVo> collect = page.getRecords().stream().map(item -> {
            PostVo postVo = new PostVo();
            postService.getPostList(false,postVo,item);
            return postVo;
        }).collect(Collectors.toList());
        return collect;
    }
}
